<!DOCTYPE html>
<html>
<?php $this->load->view('admin/public/header.html');?>
<body class="childrenBody" style="width:50%">

	<blockquote class="layui-elem-quote quoteBox">		
		<div class="layui-inline" style="margin-right: 50px;font-size: 20px">分类管理</div>
		<div class=" layui-inline layui-btn addType ">添加分类</div>
	</blockquote>
	
	<table class="layui-table" lay-filter="typeList">
	  <colgroup>
	    <col width="150">
	    <col width="50">
	  </colgroup>
	  <thead>
	    <tr>
	      <th lay-data="{field:'giftId',width:'10%'}">ID</th>
	      
	      <th  lay-data="{field:'url',width:'',edit:'text'}">礼物图片地址</th>
	      <th lay-data="{field:'name',width:'',edit:'text'}">礼物名称</th>
	      <th lay-data="{field:'price',width:'',edit:'text'}">礼物价格</th>
	      <th lay-data="{title: '操作',width:'20%', templet:'#newsListBar',fixed:'right',align:'center'}">操作</th>
	    </tr> 
	  </thead>
	  <tbody>
	  	<?php foreach($gift as $key=>$val):?>
	    <tr>
	      <td><?php echo $val['giftId']?></td>
	      <td ><?php echo @$val['url']?></td>
	      <td><?php echo $val['name']?></td>
	      <td><?php echo $val['price']?></td>
	    </tr>
	    <?php endforeach;?>
	  </tbody>
	</table>
	

<!--操作-->
<script type="text/html" id="newsListBar">
		<a class="layui-btn layui-btn-xs " lay-event="edit">编辑</a>		
		<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>		
</script>
<script type="text/javascript">
layui.use(['form','layer','table','laytpl'],function(){
	var table = layui.table;	
	$ = layui.jquery,
	//转换静态表格
	table.init('typeList');
	table.on('edit(typeList)', function(obj){ //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
		  
		  $.ajax({
			  type:'post',
			  data:obj.data,
			  url:'editType',
			  success:function(res){
				  if(res!=200){
					  layer.msg('修改失败');
				  }
			  },
			  fail:function(){
				  layer.msg('修改失败');
			  }
		  })
	});
	//列表操作
    table.on('tool(typeList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;	
        console.log(data);
        if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此资讯？',{icon:3, title:'提示信息'},function(index){
                $.ajax({
                	type:'post',
                	data:{giftId:data.giftId},
                	dataType: "json", 
                	url:"<?php echo site_url('admin/gift/delGift')?>",
                	success:function(res){
                		if(res==200){
                			obj.del();
                            layer.close(index);
                		}else{
                			layer.msg('删除失败');
                		}
                	},
                	fail:function(){
                		layer.msg('删除失败');
                	}
                })
                
            });
        }else if(layEvent==='edit'){
        	add(data);
        }
    });
  //添加资讯
    function add(data={}){
        var index = layui.layer.open({
            title : "添加分类",
            type : 2,
            content : "<?php echo site_url('admin/gift/addGift_index')?>",		//iframe层时,这个参数传URL
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                
                body.find("[name=giftId]").val(data.giftId);
                body.find("[name=name]").val(data.name);
                body.find("[name=price]").val(data.price);
                body.find("[name=url]").val(data.url);
                if(data.url.length>0){
                	body.find(".posterimg").attr('src',"<?php echo IMAGEHOST?>"+data.url);
                }
                
                
                
                
                
                setTimeout(function(){
                    layui.layer.tips('点击返回', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    $(".addType").click(function(){
        add();
    })
})

</script>
</body>
</html>